/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ACRestWebService;

import AjouConquerorBeans.BuildingSateInRBean;
import Classes.ABuildingInR;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;

/**
 * REST Web Service
 *
 * @author ajou
 */
@Path("ForGameOver")
@RequestScoped
public class ForGameoverREST {

    BuildingSateInRBean buildingSateInRBean = lookupBuildingSateInRBeanBean();
    @Context
    private UriInfo context;

    public ForGameoverREST() {
    }

    @POST
    @Path("addroom")
    @Produces("application/json")
    @Consumes("text/plain")
    public String addroom(String roomid) {
        int tpforroomid = Integer.parseInt(roomid);
        buildingSateInRBean.addbuildng(tpforroomid);

        return "added";
    }

    @POST
    @Path("concueredAB")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String concueredAB(@FormParam("uid") String uid, @FormParam("roomid") String roomid, @FormParam("buildingname") String buildingname, @FormParam("point") String point) {
        int tpforroomid = Integer.parseInt(roomid);
        int tpforpoint = Integer.parseInt(point);
        buildingSateInRBean.concquer(buildingname, uid, tpforpoint, tpforroomid);

        return buildingname;
    }

    @POST
    @Produces("application/json")
    @Consumes("application/json")
    public String getGameover(String roomid) {
        int r = Integer.parseInt(roomid);
        if (buildingSateInRBean.checkGameover(r))
            return "true";
        return "false";
    }

    @POST
    @Path("returnbuildings")
    @Produces("application/json")
    @Consumes("text/plain")
    public ArrayList<ABuildingInR> returnbuildings(String roomid) {
        ArrayList<ABuildingInR> tp = new ArrayList<ABuildingInR>();
        int roomi = Integer.parseInt(roomid);
        tp = buildingSateInRBean.returnbuildings(roomi);
        return tp;
    }

    @GET
    @Produces("application/xml")
    public String putXml() {
        //TODO return proper representation object
        throw new UnsupportedOperationException();
    }

    @GET
    @Consumes("application/xml")
    public String getXml() {
        throw new UnsupportedOperationException();
    }

    private BuildingSateInRBean lookupBuildingSateInRBeanBean() {
        try {
            javax.naming.Context c = new InitialContext();
            return (BuildingSateInRBean) c.lookup("java:global/AjouConquerorServer/AjouConquerorServer-ejb/BuildingSateInRBean!AjouConquerorBeans.BuildingSateInRBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
